package util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter(filterName = "Resquet_utf8",urlPatterns = ("/*"))

public class Resquet_utf8 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request_Servlet,
                         ServletResponse response_Servlet,
                         FilterChain chain)
            throws IOException, ServletException {
        System.out.println("Resquet_utf8 - begins");
        HttpServletRequest request = (HttpServletRequest) request_Servlet;
        HttpServletResponse response = (HttpServletResponse) response_Servlet;

        String path = request.getRequestURI();

        if (!path.contains("/login")){
            //设置响应字符编码为UTF-8
            response.setContentType("application/json;charset=UTF-8");

            String methon = request.getMethod();

            if ("POST-PUT-GET".contains(methon)) {
                //设置请求字符编码为UTF-8
                request.setCharacterEncoding("UTF-8");

                //System.out.println("登录成功"+verify.toString());
            }

            System.out.println("Resquet_utf8 - encoding ends");
        }
        chain.doFilter(request_Servlet,response_Servlet);
    }

    @Override
    public void destroy() {

    }
}
